package com.jky.stmall.manger.config;

import com.jky.stmall.manger.interceptor.AdminInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置 SpringMVC
 */
//该注解 声明当前类是一个配置类，相当于 Spring 中的一个 XML 文件
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${upload.path}")
    private String path;
    @Autowired
    private AdminInterceptor adminInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //添加上传图片的映射路径
        //当客户端访问upload 路径下 任何资源时将从下面的 本地资源 file中获取
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:"+path);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册管理员的
        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/**")//添加要拦截的路径
                .excludePathPatterns("/layui/**","/admin/login.html","/vercode","/api/admin/login");//排除不需要拦截的路径
    }
}
